﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Globalization;
using QASampling.BusinessObject;
using QASampling.Util;
using QASampling.UI;

namespace QASampling.QAControl
{
    public partial class QATextEdit : UserControl
    {
        private DataTable dataTable;
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private string labelText;
        public string LabelText
        {
            get
            {
                return labelText;
            }
            set
            {
                labelText = value;
                NotifyPropertyChanged("LabelText");
            }
        }

        private string tableSource;
        public string TableSource
        {
            get
            {
                return tableSource;
            }
            set
            {
                tableSource = value;
                NotifyPropertyChanged("TableSource");
            }
        }

        private string fieldName = string.Empty;
        public string FieldName
        {
            get
            {
                return fieldName;
            }
            set
            {
                fieldName = value;
                NotifyPropertyChanged("FieldName");
            }
        }

        private int fieldLength;
        public int FieldLength
        {
            get
            {
                return fieldLength;
            }
            set
            {
                fieldLength = value;
                NotifyPropertyChanged("FieldLength");
            }
        }

        private JenisField fieldType;
        public JenisField FieldType
        {
            get
            {
                return fieldType;
            }
            set
            {
                fieldType = value;
                NotifyPropertyChanged("FieldType");
            }
        }

        private string fieldRules;
        public string FieldRules
        {
            get
            {
                return fieldRules;
            }
            set
            {
                fieldRules = value;
                NotifyPropertyChanged("FieldRules");
            }
        }

        private string dateFormat;
        public string DateFormat
        {
            get { return dateFormat; }
            set
            {
                dateFormat = value;
                NotifyPropertyChanged("DateFormat");
            }
        }

        private bool readOnly = false;
        public bool ReadOnly
        {
            get { return readOnly; }
            set
            {
                readOnly = value;
                NotifyPropertyChanged("ReadOnly");
            }
        }

        private int digits = 0;
        public int Digits
        {
            get { return digits; }
            set
            {
                digits = value;
                NotifyPropertyChanged("Digits");
            }
        }

        public string FieldValue
        {
            get { return qaTextbox1.FieldValue; }
        }

        public override string Text
        {
            get { return qaTextbox1.Text; }
        }

        public QATextEdit()
        {
            InitializeComponent();
            this.AutoSize = true;
            qaEditButton1.Text = string.Empty;
            qaEditButton1.Visible = !(QAController.ReadOnly || readOnly);
            this.PropertyChanged += OnPropertyChanged;
        }

        void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "LabelText")
            {
                qaEditButton1.FieldLabel = labelText;
                qaTextbox1.LabelText = labelText;
            }
            else if (e.PropertyName == "TableSource")
            {
                qaEditButton1.TableSource = tableSource;
            }
            else if (e.PropertyName == "FieldName")
            {                
                qaTextbox1.Name = fieldName;
                qaEditButton1.FieldName = fieldName;
            }
            else if (e.PropertyName == "FieldType")
            {
                qaTextbox1.FieldType = fieldType;
                qaEditButton1.FieldType = fieldType;
            }
            else if (e.PropertyName == "FieldLength")
            {
                qaEditButton1.FieldLength = fieldLength;
            }
            else if (e.PropertyName == "FieldRules")
            {
                qaTextbox1.FieldRules = fieldRules;
            }
            else if (e.PropertyName == "DateFormat")
            {                
                qaEditButton1.DateFormat = dateFormat;
                qaTextbox1.DateFormat = dateFormat;
            }
            else if (e.PropertyName == "Digits")
            {
                qaTextbox1.Digits = digits;
                qaEditButton1.Digits = digits;
            }
            else if (e.PropertyName == "ReadOnly")
            {
                qaEditButton1.Visible = !(QAController.ReadOnly || readOnly);
            }
        }

     /*   private void setbinding()
        {
            // Gets the attributes for the property.
            AttributeCollection attributes =
               TypeDescriptor.GetProperties(this)["DateFormat"].Attributes;


            // This is another way to see whether the property is browsable.
            BrowsableAttribute myAttribute =
               (BrowsableAttribute)attributes[typeof(BrowsableAttribute)];

            FieldInfo fi = myAttribute.GetType().GetField("Browsable", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase);

            fi.SetValue(myAttribute, true);
        }*/

        private void QALabelTextEdit_Load(object sender, EventArgs e)
        {
            try
            {
                this.dataTable = QAController.GetSPTWithField(tableSource, fieldName);
               
                if (fieldType.Equals(JenisField.Datetime))
                {
                    Binding dobBinding = new Binding("Text", dataTable, fieldName);
                    dobBinding.Format += new ConvertEventHandler(dobBinding_Format);
                    this.qaTextbox1.DataBindings.Add(dobBinding);
                    this.qaTextbox1.OriginalValue = qaTextbox1.Text;
                    //this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName, true, DataSourceUpdateMode.OnPropertyChanged, null, dateFormat);
                    //this.qaTextbox1.DataBindings.Add("OriginalValue", dataTable, fieldName, true, DataSourceUpdateMode.OnPropertyChanged, null, dateFormat);
                }
                else if (fieldType.Equals(JenisField.Currency))
                {
                    Binding currBinding = new Binding("Text", dataTable, fieldName);
                    currBinding.Format += new ConvertEventHandler(currBinding_Format);
                    this.qaTextbox1.DataBindings.Add(currBinding);
                    this.qaTextbox1.OriginalValue = qaTextbox1.Text;
                    //this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName, true, DataSourceUpdateMode.OnPropertyChanged, null, dateFormat);
                    //this.qaTextbox1.DataBindings.Add("OriginalValue", dataTable, fieldName, true, DataSourceUpdateMode.OnPropertyChanged, null, dateFormat);
                }
                else
                {
                    this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName);
                    this.qaTextbox1.DataBindings.Add("OriginalValue", dataTable, fieldName);
                }
                this.qaTextbox1.DataBindings.Add("FieldValue", dataTable, fieldName);
                this.qaTextbox1.Form = (BaseFormSPTWithHeader) this.FindForm();
            }
            catch (Exception)
            {
                return;
            }
        }

        void dobBinding_Format(object sender, ConvertEventArgs e)
        {
            // We only deal with converting to dates and strings
            if (e.DesiredType != typeof(string)) return;
            if (e.Value.GetType() != typeof(DateTime)) return;

            DateTime value = (DateTime)e.Value;

            try
            {
                e.Value = value.ToString(this.dateFormat);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        void currBinding_Format(object sender, ConvertEventArgs e)
        {
            // We only deal with converting to dates and strings
            if (e.DesiredType != typeof(string)) return;
            if (e.Value.GetType() != typeof(decimal)) return;

            decimal value = (decimal)e.Value;

            try
            {
                string numericFormat = "N" + digits;
                string currency = value.ToString(numericFormat, CultureInfo.GetCultureInfo("id-ID"));
                if (currency.StartsWith("-"))
                {
                    currency = "(" + currency + ")";
                    currency = currency.Replace("-", "");
                }
                e.Value = currency;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        private void qaEditButton1_Click(object sender, EventArgs e)
        {
            try
            {
                this.dataTable = QAController.GetSPTWithField(tableSource, fieldName);                
                this.qaTextbox1.DataBindings.Clear();
                if (fieldType.Equals(JenisField.Datetime))
                    this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName, true, DataSourceUpdateMode.OnPropertyChanged, null, dateFormat);
                else if (fieldType.Equals(JenisField.Currency))
                {
                    //this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName, true, DataSourceUpdateMode.OnPropertyChanged, null, "N0", CultureInfo.GetCultureInfo("id-ID"));
                    Binding currBinding = new Binding("Text", dataTable, fieldName);
                    currBinding.Format += new ConvertEventHandler(currBinding_Format);
                    this.qaTextbox1.DataBindings.Add(currBinding);
                }
                else
                    this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName);
                this.qaTextbox1.DataBindings.Add("FieldValue", dataTable, fieldName);
                this.qaTextbox1.Focus();
            }
            catch (Exception)
            {
                return;
            }
        }

        private void qaTextbox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                qaEditButton1.PerformClick();
            }
        }       
    }
}
